package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Old Zhang
 */
@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}


	@EnableAsync
	@Configuration
	class TaskPoolConfig{

		@Bean("taskExecutor")
		public Executor taskExecutor(){
			ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
			//核心线程数
			executor.setCorePoolSize(10);
			//最大线程数
			executor.setMaxPoolSize(100);
			//缓冲队列
			executor.setQueueCapacity(200);
			//允许线程的空闲时间当前为60秒
			executor.setKeepAliveSeconds(60);
			executor.setThreadNamePrefix("taskThread-");
			executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
			return executor;
		}
	}
}
